package mx.ipn.presentacion.nomina;

import mx.ipn.presentacion.nomina.ui.*;
import mx.ipn.presentacion.ConexionConNegocios;
import com.trolltech.qt.gui.QMessageBox;
import com.trolltech.qt.gui.QApplication;
import com.trolltech.qt.gui.QTableWidgetItem;
import com.trolltech.qt.gui.QWidget;
import com.trolltech.qt.gui.QDialog;
import mx.ipn.to.*;

import java.util.*;
public class CrearDP extends QDialog{
	Ui_Creardesc crear;
	DescuentoTO desc;
	PercepcionTO per;
	TipoPercepcionDescuentoTO tipopd;
	TipoPercepcionDescuentoTO [] arrtipopd;
	int id_desc;
	int id_dp;
	String nombre;
	String descripcion;
	double val;
	int tablaper=-1;
	int bandera = -1;
	public CrearDP(){
			crear = new Ui_Creardesc();
			crear.setupUi(this);
			//rep.tableWidget.setRowCount(1);
			//rep.tableWidget.itemClicked.connect(this,"selectFactura()");
			//rep.pushButton.clicked.connect(this, "ver()");
			//rep.pushButton_2.clicked.connect(this, "buscar()");
			//rep.radioButton.clicked.connect(this,"mes()");
			//rep.radioButton_2.clicked.connect(this,"dia()");
			crear.pushButton.clicked.connect(this, "modificar()");
			crear.buttonBox_2.accepted.connect(this, "accept()");
            crear.buttonBox_2.rejected.connect(this, "reject()");
            crear.radioButton.clicked.connect(this,"tablaper()");
			crear.radioButton_2.clicked.connect(this,"tabladesc()");
			llenacombo();
			this.show();
	}
	public CrearDP(QDialog parent){
			
	}
	public void limpia(){
		crear.lineEdit.setText("");
		crear.lineEdit_2.setText("");
	}
	/*public void eliminar(){
		short r1= (short)-1;
		if(tableper==0){
		r1 =((Short)ConexionConNegocios.invocaServicio("eliminaDetalleFactura",idf,Short.class)).shortValue();
	    }
	    if(tableper==1){
	    r1 =((Short)ConexionConNegocios.invocaServicio("eliminaDetalleFactura",idf,Short.class)).shortValue();
	    }
		if(r==1){
			 QMessageBox.information(null,"informacion","eliminada");
			 limpia();
	         }
		else{
			 QMessageBox.critical(null, "Error",
						"No se borro nungun dato.",
						QMessageBox.StandardButton.Ok,
						QMessageBox.StandardButton.Ok);}
	}*/
	public void llena(DescuentoTO desc){
		crear.lineEdit.setText(desc.getNombre());
		crear.lineEdit_2.setText(desc.getDescripcion());
		crear.lineEdit_3.setText(String.valueOf(desc.getValor()));
	}
	public void llena1(PercepcionTO per){
		crear.lineEdit.setText(per.getNombre());
		crear.lineEdit_2.setText(per.getDescripcion());
		crear.lineEdit_3.setText(String.valueOf(per.getValor()));
	}
	public void modificar(){
		bandera=1;
		Modificar mod = new Modificar();
		if(mod.exec()== QDialog.DialogCode.Accepted.value()){
			if(tablaper==0){
				desc=mod.desc;
				if(mod.desc!=null)
		         llena(mod.desc);
			}
			if(tablaper==1){
				per=mod.per;
				if(mod.desc!=null)
		         llena1(mod.per);
			}
			
		}
	}
	public TipoPercepcionDescuentoTO buscarTipo(String nombre){
		TipoPercepcionDescuentoTO  tipopd=null;
		for(int i=0;i<arrtipopd.length;i++){
			if(arrtipopd[i].getNombre().compareToIgnoreCase(nombre)==0){
				tipopd=arrtipopd[i];
			}
		}
		return tipopd;
	}
	public void llenacombo(){
		arrtipopd=(TipoPercepcionDescuentoTO[])ConexionConNegocios.invocaServicio("selectTipoPD",TipoPercepcionDescuentoTO[].class);
		if(arrtipopd!=null){
			for (TipoPercepcionDescuentoTO f : arrtipopd)
		    crear.comboBox.addItem(f.getNombre());
	    }
	}
	public void tablaper(){
		tablaper=1;
	}
	public void tabladesc(){
		tablaper=0;
	}
	public boolean valida(){
		boolean valido=false;
		if(crear.lineEdit.text().compareToIgnoreCase("")!=0){
			if(crear.lineEdit_2.text().compareToIgnoreCase("")!=0){
					valido=true;		
			}
		}
		return valido;
	}
	public void accept(){
		nombre = crear.lineEdit.text();
		descripcion = crear.lineEdit_2.text();
		val= Double.parseDouble(crear.lineEdit_3.text());
		if(bandera==1){
			if(tablaper!=-1){
			  tipopd=buscarTipo(crear.comboBox.currentText());
			  if(valida()){
			  	 //si descuento
			  	 if(tablaper==0){
			        short r=((Short)ConexionConNegocios.invocaServicio("updateDescuento",desc,Short.class)).shortValue();
			        if(r==1)
			           QMessageBox.information(null,"informacion","guardada");
			     }
			     //si percepcion
			     if(tablaper==1){
			        short r1=((Short)ConexionConNegocios.invocaServicio("updatePercepcion",per,Short.class)).shortValue();
			        if(r1==1)
			           QMessageBox.information(null,"informacion","guardada");
			     }
		      }
		    }
			bandera=-1;
		}
		if(bandera!=1){
		if(tablaper!=-1){
		  tipopd=buscarTipo(crear.comboBox.currentText());
		  if(valida()){
		  	 //si descuento
		  	 if(tablaper==0){
		        desc = new DescuentoTO(0,nombre,descripcion,val,tipopd);
		        boolean flag=((Boolean)ConexionConNegocios.invocaServicio("addDescuento",desc,Boolean.class)).booleanValue();
		        if(flag)
		           QMessageBox.information(null,"informacion","guardada");
		     }
		     //si percepcion
		     if(tablaper==1){
		        per = new PercepcionTO(0,nombre,descripcion,val,tipopd);
		        boolean flag1=((Boolean)ConexionConNegocios.invocaServicio("insertPercepcion",per,Boolean.class)).booleanValue();
		        if(flag1)
		           QMessageBox.information(null,"informacion","guardada");
		     }
	      }
	    }
	    }
	}
	public void reject(){
		this.dispose();
	}
}